package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.PersonalScheduleVO;
import edu.zqu.instructionalMatters.project.entity.PersonalSchedule;
import edu.zqu.instructionalMatters.project.mapper.PersonalScheduleMapper;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 个人日程表 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "个人日程")
public class PersonalScheduleController {
    @Autowired
    private PersonalScheduleMapper personalScheduleMapper;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取个人日程信息列表")
    @RequestMapping(value = "/personalSchedules", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = false)
    })
    public ZQUResult get(Date theDate,
                         BigInteger userId) {
        PersonalSchedule personalSchedule1 = new PersonalSchedule().setTheDate(theDate).setUserId(userId);               //合成对象
        List<PersonalSchedule> list1 = personalScheduleMapper.findAll(personalSchedule1);                                //获取 List<PersonalSchedule>
        List<PersonalScheduleVO> list = new ArrayList<>();                                                              //初始化List<PersonalScheduleVO>
        for (PersonalSchedule personalSchedule : list1) {                                                               //循环遍历
            PersonalScheduleVO personalScheduleVO = new PersonalScheduleVO();                                           //初始化 VO
            BeanUtils.copyProperties(personalSchedule, personalScheduleVO);                                              //对象复制赋值
            String userName = userService.getUserName(personalSchedule.getUserId());                                    //获取用户名
            personalScheduleVO.setUserName(userName);                                                                   //设置用户名
            list.add(personalScheduleVO);                                                                               //加入 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取个人日程信息", response = PersonalScheduleVO.class)
    @RequestMapping(value = "/personalSchedule/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "个人日程id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        PersonalSchedule personalSchedule = personalScheduleMapper.selectById(id);               //查询个人日程对象
        String userName = userService.getUserName(personalSchedule.getUserId());                 //获取用户名
        PersonalScheduleVO personalScheduleVO = new PersonalScheduleVO();                        //初始化VO
        BeanUtils.copyProperties(personalSchedule, personalScheduleVO);                           //对象复制赋值
        personalScheduleVO.setUserName(userName);                                                //设置用户名
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", personalScheduleVO);           //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/personalSchedule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = true),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query", required = false),
            @ApiImplicitParam(name = "isShow", value = "是否可见", paramType = "query", required = false),
            @ApiImplicitParam(name = "level", value = "等级", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger userId,
                         Date theDate,
                         String startTime,
                         String endTime,
                         String name,
                         String description,
                         Integer isShow,
                         Integer level,
                         BigInteger byWho) {
        PersonalSchedule personalSchedule = new PersonalSchedule();               //初始化
        personalSchedule.setUserId(userId);                                       //设置用户id
        personalSchedule.setTheDate(theDate);                                     //设置日期
        personalSchedule.setStartTime(Timestamp.valueOf(startTime));              //设置开始时间
        personalSchedule.setEndTime(Timestamp.valueOf(endTime));                  //设置结束时间
        personalSchedule.setName(name);                                           //设置行程名称
        personalSchedule.setDescription(description);                             //设置行程描述
        personalSchedule.setIsShow(isShow);                                       //设置是否显示
        personalSchedule.setLevel(level);                                         //设置轻重程度
        personalSchedule.setByWho(byWho);                                         //设置操作者
        personalScheduleMapper.insert(personalSchedule);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/personalSchedule/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = true),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query", required = false),
            @ApiImplicitParam(name = "isShow", value = "是否可见", paramType = "query", required = false),
            @ApiImplicitParam(name = "level", value = "等级", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(@PathVariable BigInteger id,
                            BigInteger userId,
                            Date theDate,
                            String startTime,
                            String endTime,
                            String name,
                            String description,
                            Integer isShow,
                            Integer level,
                            BigInteger byWho) {
        PersonalSchedule personalSchedule = personalScheduleMapper.selectById(id);//先查出旧记录
        personalSchedule.setByWho(byWho);                                         //修改操作者
        personalScheduleMapper.updateById(personalSchedule);                      //更新操作者
        personalScheduleMapper.deleteById(id);                                    //逻辑删除
        personalSchedule = new PersonalSchedule();                                //初始化新纪录
        personalSchedule.setUserId(userId);                                       //设置用户id
        personalSchedule.setTheDate(theDate);                                    //设置日期
        personalSchedule.setStartTime(Timestamp.valueOf(startTime));              //设置开始时间
        personalSchedule.setEndTime(Timestamp.valueOf(endTime));                  //设置结束时间
        personalSchedule.setName(name);                                           //设置行程名称
        personalSchedule.setDescription(description);                             //设置行程描述
        personalSchedule.setIsShow(isShow);                                       //设置是否显示
        personalSchedule.setLevel(level);                                         //设置轻重程度
        personalSchedule.setByWho(byWho);                                         //设置操作者
        personalSchedule.setLastVersion(id);                                      //设置上一版本
        personalScheduleMapper.insert(personalSchedule);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/personalSchedule/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        PersonalSchedule personalSchedule = personalScheduleMapper.selectById(id);//先查出旧记录
        personalSchedule.setByWho(byWho);                                         //修改操作者
        personalScheduleMapper.updateById(personalSchedule);                      //更新操作者
        personalScheduleMapper.deleteById(id);                                    //逻辑删除
        return ZQUResult.success();                                               //操作成功返回
    }
}

